package com.idbk.chargestation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationURL;
import com.idbk.chargestation.bean.JsonCarBrands;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 汽车品牌选择适配器
 * @author liangzw,lupc
 *
 */
public class CarBrandAdapter extends BaseAdapter {

	private Context mContext;
	private List<JsonCarBrands> list;//汽车品牌集合
	private LayoutInflater mInflater;
	
	public CarBrandAdapter(List<JsonCarBrands> list, Context context) {
		mContext = context;
		this.list = list;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {

		return list.size();
	}

	@Override
	public Object getItem(int position) {

		return list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	private final class Item {
		ImageView tvLogo;
		TextView tvName;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final JsonCarBrands brands = list.get(position);
		Item item;
		if(convertView == null) {
			item = new Item();
			convertView = mInflater.inflate(R.layout.item_car_brands, parent, false);
			item.tvLogo = (ImageView) (convertView).findViewById(R.id.car_brand_logo);
			item.tvName = (TextView) (convertView).findViewById(R.id.car_brand_name);
			convertView.setTag(item);//缓存组件
		} else {
			item = (Item) convertView.getTag();
		}
		
		//获取图片资源
		String url = ChargeStationURL.getAbsoluteApiUrlFromRoot(brands.logoURL);
		Picasso.with(mContext)
		.load(url)
		.into(item.tvLogo);
		
		item.tvName.setText(brands.brandName);//汽车品牌名称
		return convertView;
	}

}
